<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<xsl:template match="Form" mode="system">
		<xsl:variable name="toPage">
			<xsl:choose>
				<xsl:when test="@Page">
					<xsl:value-of select="@Page"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$Page"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<form id="{@ID}" name="{@Name}" action="{$Self}/{$toPage}/{@Action}" method="{@Method}">
			<xsl:copy-of select="@*[ not(name() = 'ID') and not(name() = 'Name') and not(name() = 'Method') and not(name() = 'Page') and not(name() = 'Action') ]" />
			<xsl:if test="//Field/File">
 				<xsl:attribute name="enctype">multipart/form-data</xsl:attribute>
			</xsl:if>
			<xsl:if test="@onsubmit">
				<xsl:attribute name="onsubmit"><xsl:value-of select="@onsubmit"/></xsl:attribute>
			</xsl:if>
			<xsl:if test="@onkeypress">
				<xsl:attribute name="onkeypress"><xsl:value-of select="@onkeypress"/></xsl:attribute>
			</xsl:if>
			<div class="Form">
				<xsl:apply-templates/>
			</div>
		</form>
	</xsl:template>

	<xsl:template match="FormSection">
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="FormSection[ @Group='Bar' ]">
		<div class="FormBar">
			<xsl:apply-templates/>
		</div>
	</xsl:template>

	<!-- default Field match -->
	<xsl:template match="Field">
		<dl class="Field">
			<xsl:choose>
				<xsl:when test="@Required = 1">
					<xsl:attribute name="class">Field Required</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="class">Field</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<!-- label is an html construct that refers to a particular field -->
			<label for="{Name}">
				<xsl:value-of select="Label"/>
			</label>
			<!-- determine what type of field to create -->
			<dt class="Input">
				<input type="text" name="{Name}" id="{Name}">
					<xsl:attribute name="value">
						<xsl:call-template name="getFieldValue">
							<xsl:with-param name="Field" select="."/>
						</xsl:call-template>
					</xsl:attribute>
					<xsl:for-each select="Params/*">
						<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
					</xsl:for-each>
				</input>
			</dt>
			<!-- if we have a description, show it -->
			<xsl:if test="Description">
				<dd class="Info">
					<xsl:apply-templates select="Description"/>
				</dd>
			</xsl:if>
		</dl>
	</xsl:template>

	<!-- any Field with options -->
	<xsl:template match="Field[ FieldOptions ]">
		<dl class="Field">
			<xsl:choose>
				<xsl:when test="@Required = 1">
					<xsl:attribute name="class">Field Required</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="class">Field</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<!-- label is an html construct that refers to a particular field -->
			<label for="{Name}">
				<xsl:value-of select="Label"/>
			</label>
			<!-- determine what type of field to create -->
			<dt class="Input">
				<xsl:choose>
					<!-- it's a pulldown -->
					<xsl:when test="not(FieldOptions/@Select) or FieldOptions/@Select = 'one'">
						<select name="{Name}" id="{Name}">
							<xsl:for-each select="Params/*">
								<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
							</xsl:for-each>
							<xsl:apply-templates select="FieldOptions">
								<xsl:with-param name="SelectedVal" select="Value"/>
							</xsl:apply-templates>
						</select>
					</xsl:when>
				</xsl:choose>
			</dt>
			<!-- if we have a description, show it -->
			<xsl:if test="Description">
				<dd class="Info">
					<xsl:apply-templates select="Description"/>
				</dd>
			</xsl:if>
		</dl>
	</xsl:template>

	<xsl:template match="Field[ @Type = 'textarea' ]">
		<dl class="Field">
			<xsl:choose>
				<xsl:when test="@Required = 1">
					<xsl:attribute name="class">Field Required</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="class">Field</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<!-- label is an html construct that refers to a particular field -->
			<label for="{Name}" style="float: none;">
				<xsl:value-of select="Label"/>
			</label>
			<!-- determine what type of field to create -->
			<dt class="Input" style="float: none;">
				<textarea name="{Name}" id="{Name}">
					<xsl:for-each select="Params/*">
						<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
					</xsl:for-each>
				</textarea>
			</dt>
			<!-- if we have a description, show it -->
			<xsl:if test="Description">
				<dd class="Info">
					<xsl:apply-templates select="Description"/>
				</dd>
			</xsl:if>
		</dl>
	</xsl:template>

	<xsl:template match="Field[ @Type = 'hidden' ]">
		<input type="hidden" name="{Name}" id="{Name}">
			<xsl:attribute name="value">
				<xsl:call-template name="getFieldValue">
					<xsl:with-param name="Field" select="."/>
				</xsl:call-template>
			</xsl:attribute>
		</input>
	</xsl:template>

	<xsl:template match="Button">
		<input type="button" value="{Value}" style="float: none;">
			<xsl:for-each select="Params/*">
				<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
			</xsl:for-each>
		</input>
	</xsl:template>

	<!-- default FieldOptions template...called when no As attribute is specified -->
	<xsl:template match="FieldOptions | FieldOptions[ @As = 'select' ]">
		<xsl:param name="SelectedVal" select="''"/>
		<xsl:choose>
			<!-- if we have a source, read options from the source file -->
			<xsl:when test="@Source">
				<xsl:variable name="List" select="document(concat($Layout/Server/Data,@Source))/*"/>
				<xsl:if test="count($List) = 0">
					<xsl:message terminate="no">
						<option>We were unable to locate the requested file: <xsl:value-of select="concat($Layout/Server/Data,@Source)"/></option>
					</xsl:message>
				</xsl:if>
				<!-- factor in the default item which may be present in the source file -->
				<xsl:variable name="FinalSelection">
					<xsl:choose>
						<xsl:when test="string-length($SelectedVal) = 0">
							<xsl:value-of select="$List/*[ @Default = 1 ]/Value"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$SelectedVal"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:for-each select="$List/*">
					<option value="{Value}">
						<xsl:if test="Value = $FinalSelection">
							<xsl:attribute name="selected">true</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="Label"/>
					</option>
				</xsl:for-each>
			</xsl:when>
			<!-- otherwise, loop thru in-context options -->
			<xsl:when test="count(*)">
				<xsl:variable name="FinalSelection">
					<xsl:choose>
						<xsl:when test="string-length($SelectedVal) = 0">
							<xsl:value-of select="*[ @Default = 1 ]/Value"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$SelectedVal"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:for-each select="*">
					<option value="{Value}">
						<xsl:if test="Value = $FinalSelection">
							<xsl:attribute name="selected">true</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="Label"/>
					</option>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				No options, and no source in match=FieldOptions
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="FieldOptions[ @As = 'checkbox' ]">
		<xsl:param name="SelectedVal" select="''"/>
		checkbox FieldOptions not implemented yet
	</xsl:template>

	<xsl:template match="FieldOptions[ @As = 'radio' ]">
		<xsl:param name="SelectedVal" select="''"/>
		radio FieldOptions not implemented yet
	</xsl:template>

	<xsl:template name="getFieldValue">
		<xsl:param name="Field"/>
		<xsl:variable name="FieldName" select="$Field/Name"/>
		<xsl:choose>
			<!-- check in the Full request -->
			<xsl:when test="string-length($Request/Full/*[ name() = $FieldName ]) &gt; 0">
				<xsl:value-of select="$Request/Full/*[ name() = $FieldName ]"/>
			</xsl:when>
			<!-- check in the Flattened request -->
			<xsl:when test="string-length($Request/Flat/*[ @Name = $FieldName ]) &gt; 0">
				<xsl:value-of select="$Request/Flat/*[ @Name = $FieldName ]"/>
			</xsl:when>
			<!-- check in FormData -->
			<xsl:when test="string-length(//FormData/*[ @Name = $FieldName ]) &gt; 0">
				<xsl:value-of select="//FormData/*[ @Name = $FieldName ]"/>
			</xsl:when>
			<!-- default to whatever is in the field -->
			<xsl:otherwise>
				<xsl:value-of select="$Field/Value"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--
	Finds all <Field> nodes and returns a new node for each containing the value of the <Name> node.
	Includes the <Value> node also for defaulting purposes.
	This is used to inject field values into the gxpage-runtime node for use by the Forms.xsl stylesheet.
	-->
	<xsl:template match="FormData">
		<xsl:variable name="filedata" select="document(@File)"/>
		<Fields>
			<xsl:for-each select="$filedata//descendant-or-self::Field">
				<Field>
					<Name><xsl:value-of select="Name"/></Name>
					<Value><xsl:value-of select="Value"/></Value>
				</Field>
			</xsl:for-each>
		</Fields>
	</xsl:template>

</xsl:stylesheet>
